package org.example.softwarearchitecture.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.example.softwarearchitecture.dox.User;
import org.example.softwarearchitecture.exception.XException;
import org.example.softwarearchitecture.service.UserService;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RequiredArgsConstructor
@RestController
@Slf4j
@RequestMapping("/api/")
public class LoginController {

    private final UserService userService;

    @PostMapping("login")
    public Map<String,Object> login(@RequestBody User u, HttpServletResponse response) {
        log.debug("{}",u);
        User user = userService.getUser(u.getAccount(),u.getPassword());
        if(user == null) {
            throw new XException("账号密码错误");
        }
        response.addHeader("role",user.getRole().toString());
        response.addHeader("uid",user.getId().toString());
        return Map.of("user",user);
    }



}
